package com.kdtech.analyse.app;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

/**
 * @链接 http://www.huochai.mobi/u/mg/
 * @网站 火柴 APP
 * @作者 dhy
 */
public class HuochaiAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.huochai.mobi/p/d/[0-9]*/",
		"http://www.huochai.mobi/p/d/[0-9]*/\\?share_uid=[0-9]*",
		"http://www.huochai.mobi/p/d/[0-9]*/\\?.*share_tid=[a-zA-Z0-9].*",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		title =doc.select("h1.title").text();

		date=DateUtils.matchDate(doc.select("p.author").text());
		if(date==null)
			date=DateUtils.matchDate(doc.select("span.time").text());

		content =HtmlCleaner.getContentHtml(url, doc.select("div.con"));

		author=JSoupUtils.matchAuthor(doc, "作者：");
		if(StringUtils.isBlank(author))
			author=doc.select("p.txt a").text();

		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	

	

	
}

